$(function () {
    obj.init();
});

var obj = new function() {
    'use strict';
    return {
        dataGrid: null,
        init: function () {
            this.dataGrid = new xhDataGrid($('#list'), 'Device/getBound');
            this.dataGrid.parseToolbar().init().activeBtn($('#bt_list_unbind, #bt_list_rebind'));

            this.listener();
        },
        listener: function () {
            var $this = this;
            //双击打开详情
            $this.dataGrid.onDblClickCell = function (rowIndex, field, value) {
                $this.funObj().openDetail(rowIndex, field, value);
            };
            //查询
            xhTool.bindDocKeyDown(
                $('#bt_list_search').click(function () {
                    $this.funObj().search();
                })
            );
            //解绑
            $('#bt_list_unbind').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().unbind();
            });
            //恢复绑定
            $('#bt_list_rebind').click(function () {
                if (xhExtra.buttonDisabled($(this))) return false;
                $this.funObj().rebind();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                search: function () {
                    $this.dataGrid.reload({
                        search: {
                            vague: xhExtra.getValue($('#list_vague'))
                        },
                        status: xhExtra.getValue($('#list_status'))
                    }).setTitle(null, '');
                },
                unbind: function () {
                    xhExtra.askChangeStatus(
                        'Device/unbindBound',
                        {ids: $this.dataGrid.getSelectValues('iADLId')},
                        $this.dataGrid,
                        '解除绑定？'
                    );
                },
                rebind: function () {
                    xhExtra.askChangeStatus(
                        'Device/rebindBound',
                        {ids: $this.dataGrid.getSelectValues('iADLId')},
                        $this.dataGrid,
                        '重新绑定？'
                    );
                },
                openDetail: function (rowIndex, field, value) {
                    if (field === 'sUserName') {
                        $this.dataGrid.reload({
                            status: 0,
                            userId: $this.dataGrid.command('getRows')[rowIndex].iUserId
                        }).setTitle(null, '-' + value);
                    }
                    else if (field === 'sDeviceNumber') {
                        $this.dataGrid.reload({
                            status: 0,
                            devNum: value
                        }).setTitle(null, '-' + value);
                    }
                    else {
                        return false;
                    }
                    $('#list_status').combobox('setValue', 0);
                    // $('#list_vague').textbox('setValue', value);
                    // $this.dataGrid.reload({
                    //     search: {
                    //         vague: vague,
                    //         items: [item]
                    //     },
                    //     status: 0,
                    //     userId:
                    // });
                }
            }
        }
    }
};